#include "TextureManager.h"
#include <nds.h>
#include <nds/arm9/image.h>
#include <fstream>
#include <iostream>

uint16 TextureManager::LoadTexture(const char* filename)
{
	int texture;
	sImage pcx;
	void* data;
	FILE * file;

	int width = 128;
	int height = 128;

	// open texture data
	file = fopen( filename, "rb" );
	if(file == NULL) { std::cout << "cannot open texture file:" << filename << "\n"; }

	// allocate buffer
	data = malloc( width * height * 3 );

	// read texture data
	fread( data, width * height * 3, 1, file );
	fclose( file );

	loadPCX((u8*)data, &pcx);
	image8to16(&pcx);

	// allocate a texture name
	glGenTextures( 1, &texture );
	// select texture
	glBindTexture( GL_TEXTURE_2D, texture );
	glTexImage2D(0, 0, GL_RGB, TEXTURE_SIZE_128 , TEXTURE_SIZE_128, 0, TEXGEN_TEXCOORD, pcx.image.data8);

	// free buffer
	imageDestroy(&pcx);

	return texture;
}
